<template >
  <div class="add_con_form tl" v-loading="loading">
    <el-dialog
      title="编辑管理员"
      :modal-append-to-body="false"
      :close-on-click-modal="false"
      :visible.sync="visible"
      :show="show"
      @close="$emit('update:show', false)"
      class="tl size_13"
      width="800px"
    >
      <el-form :model="data" :rules="dataYZ" ref="data" class="mar_top" label-width="100px">
        <el-form-item label="角色" prop="role_id">
          <el-select
            v-model="data.role_id"
            placeholder="请选择"
            @change="handChange"
            style="width:190px"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="账号" prop="username">
          <el-col :span="4">
            <el-input
              disabled="disabled"
              placeholder="请输入账号"
              v-model="data.username"
              style="width:190px"
              clearable
            ></el-input>
          </el-col>
        </el-form-item>

        <el-form-item label="电话" prop="tel">
          <el-col :span="4">
            <el-input placeholder="请输入手机号" v-model="data.tel" style="width:190px" clearable></el-input>
          </el-col>
        </el-form-item>

        <div class="mar_bottom_15">
          <span class="sizeColor size_13 sp_style"></span>
          <el-button
            :loading="$store.state.isLoading"
            size="medium"
            type="primary"
            @click.prevent="addUpdate('data')"
          >确定</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      options: [
        {
          value: "",
          label: ""
        }
      ],
      value: "",
      data: {
        username: "",
        last_ip: "",
        last_login: ""
      },
      loading: false,
      dataYZ: {
        tel: [{ min: 11, max: 11, message: "手机格式错误", trigger: "blur" }]
      },
      visible: this.show
    };
  },
  props: {
    show: Boolean,
    adminId: String
  },
  methods: {
    getRoleList() {
      var data = {};
      this.$api.getAdminuser.getRoleOption(data).then(res => {
        this.options = res.data.result;
      });
    },
    getAdminDetails(id) {
      var data = {};
      data["id"] = id;
      this.$api.getAdminuser.getInfo(data).then(res => {
        res.data.result.last_login = res.data.result.last_login + "000";
        this.data = res.data.result;
      });
      console.log(typeof data); //object
      console.log(data); // {id: 1, sign: "ca3ca63b05dafe8fe3ab8c719055b44c"}
    },
    handChange(val) {
      console.log(val);
      this.data.role_id = val;
    },
    addUpdate(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          var params = {};
          params["id"] = this.$route.query.id;
          params["role_id"] = this.data.role_id;
          params["tel"] = this.data.tel;
          console.log(params);
          this.$api.getAdminuser.myUpdate(this.data).then(res => {
            console.log(res);
            if (res.data.code == 200 && res.data.result != null) {
              this.$emit("parent", "这里刷新父页面");
              this.$fnc.alertSuccess({ text: "修改成功" }); //成功弹出框
              this.visible = false;
            } else {
              this.$fnc.alertError(); //失败弹出框
            }
          });
        }
      });
    }
  },
  watch: {
    show() {
      this.visible = this.show;
    },
    adminId(id) {
      console.log("id", id);
      this.getAdminDetails(id);
    }
  },
  mounted() {
    this.getRoleList();
  },
  created() {}
};
</script>
